@tool
class_name Cursor
extends Node2D



@export var color: Color = Color.GREEN
@export var radius := DataItem.create(4, 4, 6)
@export var width: float = 1
@export var pressed_amt_duration: float = 0.1

@onready var animation_player: AnimationPlayer = $AnimationPlayer


var _playing_pressed_amt: bool
var _mouse_pressed: bool


func _ready() -> void:
    if Engine.is_editor_hint(): return

    Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
    
    radius.base_value_changed.connect(
        func(_ov: float,  _nv: float) -> void:
            queue_redraw()
    )


func _process(_delta: float) -> void:
    if Engine.is_editor_hint(): return
    global_position = get_global_mouse_position()

    if _mouse_pressed and not _playing_pressed_amt:
        animation_player.pause()
        _create_attack_tween().play()
        _playing_pressed_amt = true


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton:
        _mouse_pressed = event.pressed


func _draw() -> void:
    draw_circle(Vector2.ZERO, 1, color, true)
    draw_circle(Vector2.ZERO, radius.base_value, color, false, width)


func _create_attack_tween() -> Tween:
    if _playing_pressed_amt: return

    var tween := get_tree().create_tween()
    tween.tween_property(self, "radius:base_value", radius.min_value, pressed_amt_duration)
    tween.finished.connect(
        func() -> void:
            _playing_pressed_amt = false
            animation_player.seek(0)
            animation_player.play("fade")
    )
    return tween
